package xuegao.study.mall.web.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xuegao.study.mall.web.service.UmsMemberService;
import xuegao.study.mall.common.api.CommonResult;
import xuegao.study.mall.common.model.oms.OmsCartItem;
import xuegao.study.mall.web.service.OmsCarItemService;

import java.util.List;

@RestController
@Api(tags = {"OmsCarItemController"})
@Tag(name = "OmsCarItemController", description = "购物车管理")
@RequestMapping("/cart")
public class OmsCarItemController {

    @Autowired
    private OmsCarItemService omsCarItemService;

    @Autowired
    private UmsMemberService umsMemberService;

    @ApiOperation("添加商品到购物车")
    @PostMapping("/add")
    public CommonResult add(@RequestBody OmsCartItem cartItem) {
        int count = omsCarItemService.add(cartItem);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("获取当前会员的购物车列表")
    @GetMapping("/list")
    public CommonResult<List<OmsCartItem>> list() {
        Long memberId = umsMemberService.getCurrentMember().getId();
        List<OmsCartItem> cartItemList = omsCarItemService.list(memberId);
        return CommonResult.success(cartItemList);
    }
}
